package com.iflytek.universitymanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iflytek.universitymanagement.domain.Student;
import com.iflytek.universitymanagement.domain.StudentCompetition;
import com.iflytek.universitymanagement.domain.StudentScore;
import com.iflytek.universitymanagement.exception.ResourceNotFoundException;
import com.iflytek.universitymanagement.mapper.StudentCompetitionMapper;
import com.iflytek.universitymanagement.mapper.StudentMapper;
import com.iflytek.universitymanagement.mapper.StudentScoreMapper;
import com.iflytek.universitymanagement.service.StudentCompetitionService;
import com.iflytek.universitymanagement.service.CompetitionService;
import com.iflytek.universitymanagement.domain.Competition;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

@Service
public class StudentCompetitionServiceImpl extends ServiceImpl<StudentCompetitionMapper, StudentCompetition> implements StudentCompetitionService {

    @Autowired
    private StudentCompetitionMapper studentCompetitionMapper;

    @Autowired
    private StudentScoreMapper studentScoreMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private CompetitionService competitionService;

    // 根据学生姓名查询学生的竞赛数据
    @Override
    public List<Map<String, Object>> getStudentCompetitionsByStudentName(String studentName) {
        // 根据学生姓名查询学生信息
        Student student = studentMapper.selectByName(studentName);
        if (student == null) {
            throw new ResourceNotFoundException("未找到该学生");
        }

        Integer studentId = student.getStudentId();  // 获取学生ID
        // 获取该学生的竞赛数据
        return getStudentCompetitionsWithDetails(studentId);
    }


    // 根据学生ID查询竞赛数据并封装竞赛名称
    @Override
    public List<Map<String, Object>> getStudentCompetitionsWithDetails(Integer studentId) {
        // 查询学生的竞赛数据
        List<StudentCompetition> studentCompetitions = studentCompetitionMapper.selectByStudentId(studentId);

        // 通过竞赛ID查询竞赛名称并封装结果
        List<Map<String, Object>> result = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        for (StudentCompetition studentCompetition : studentCompetitions) {
            // 通过竞赛ID获取竞赛详情
            Competition competition = competitionService.getById(studentCompetition.getCompetitionId());
            if (competition != null) {
                Map<String, Object> competitionData = new HashMap<>();
                competitionData.put("name", competition.getName());  // 添加竞赛名称
                competitionData.put("participationStatus", studentCompetition.getParticipationStatus());
                competitionData.put("awards", studentCompetition.getAwards());
                competitionData.put("url", competition.getUrl());  // 添加竞赛链接

                // 格式化日期
                if (competition.getStartDate() != null) {
                    competitionData.put("startDate", sdf.format(competition.getStartDate()));
                }

                if (competition.getEndDate() != null) {
                    competitionData.put("endDate", sdf.format(competition.getEndDate()));
                }

                result.add(competitionData);
            }
        }
        return result;
    }

    @Override
    public IPage<StudentCompetition> selectPage(int pageNum, int pageSize, String name, String competitionName) {
        QueryWrapper<StudentScore> queryWrapper = new QueryWrapper<>();
        if (name != null && !name.trim().isEmpty()) {
            queryWrapper.like("s.name", name); // Assuming 's' is the alias for the student table
        }
        if (competitionName != null && !competitionName.trim().isEmpty()) {
            queryWrapper.like("c.name", competitionName); // Assuming 'c' is the alias for the course table
        }
        queryWrapper.orderByAsc("student_competition_id");
        return studentCompetitionMapper.selectByPage(new Page<>(pageNum, pageSize), queryWrapper);
    }


        @Override
        public Map<String, Object> getStudentCompetitionData(String studentName) {
            Map<String, Object> result = new HashMap<>();

            // 根据学生姓名查询学生ID（假设有一个方法根据学生姓名查询ID）
            Integer studentId = studentMapper.getStudentIdByName(studentName);
            if (studentId == null) {
                return result; // 如果没有找到该学生，返回空结果
            }

            // 计算总参与次数
            int totalParticipations = studentCompetitionMapper.countParticipations(studentId);

            // 计算获奖总数
            int totalAwards = studentCompetitionMapper.countAwards(studentId);

            // 查询当前活跃竞赛
            int activeCompetitions = studentCompetitionMapper.countActiveCompetitions(studentId);

            // 查询最近成绩趋势
            List<StudentScore> recentScores = studentScoreMapper.getRecentScores(studentId);

            // 将查询结果返回
            result.put("totalParticipations", totalParticipations);
            result.put("totalAwards", totalAwards);
            result.put("activeCompetitions", activeCompetitions);
            result.put("recentScores", recentScores);

            return result;
        }

    @Override
    public boolean add(StudentCompetition studentCompetition) {
        return studentCompetitionMapper.insert(studentCompetition) > 0 ;
    }
    }

